﻿<div class="easyui-layout" id="Layout_dm_lap_dat_cau_hinh_tb">
    <div id="pannel_list_mang_vt_find_site" data-options="region:'west', collapsible:false" title="MẠNG VT" style="width: 300px; padding: 10px">
        
        <input value="PON" id="MangVT_FIND_DV_SITE" style="width:270px; height:27px" />

         <div id="TreeGird_dm_dvi_toql_vnsite"></div>

    </div>
    <div id="pannel_list_lap_thiet_bi" data-options="region:'center'" style="padding:5px; position:relative" title="Danh sách lắp thiết bị">
        <div id="lap_thiet_bi_TableContainer"></div>
    </div>
</div>


<script type="text/javascript">

    var madonviCurrently = "";
    var matoqlCurrently = "";
    var maiSiteCurrntly = "";
    var maiTyComGirdCurrntly = "all";
    var maTramVT_DAUNOITB_Currntly = "PON";

    $(document).ready(function () {



        $("#Layout_dm_lap_dat_cau_hinh_tb").css('width', $(window).width() - 5);
        $("#Layout_dm_lap_dat_cau_hinh_tb").height($(window).height() - 100);
        $('#lap_thiet_bi_TableContainer').height($(window).height() - 180);
        $('#lap_thiet_bi_TableContainer').width($("#pannel_list_lap_thiet_bi").width() - 315);
        
        $("#Layout_dm_lap_dat_cau_hinh_tb").mask("Dữ liệu đang tải về ");


        $("#MangVT_FIND_DV_SITE").combobox({
            url: _nameSubDomain + "/TThietbi/ListMangVt",
            valueField: "MA",
            textField: "TEN",
            disabled: true,
            onSelect: function (row) {
                maTramVT_DAUNOITB_Currntly = row.MA;
                //$("#TreeGird_dm_dvi_toql_vnsite").treegrid('reload', { tramVt: row.MA });
                $('#lap_thiet_bi_TableContainer').jtable('load', { donvi: madonviCurrently, toql: matoqlCurrently, SiteId: "", type: maiTyComGirdCurrntly, mangvt: row.MA });
                //madonviCurrently = ""; matoqlCurrently = ""; maiSiteCurrntly = "";
            },

        });

        
        $("#TreeGird_dm_dvi_toql_vnsite").treegrid({

            url: _nameSubDomain + "/TThietbi/listDVTOByTramFromDauNoi",
            idField: "id",
            treeField: "text",
            lines: true,
            animate: true,
            collapsible: true,
            fitColumns: true,
            width: $("#pannel_list_mang_vt_find_site").width() - 30,
            height: $(window).height() - 200,
            columns: [[
                { field: 'id', resizable: false, hidden: true },
                { field: 'text', resizable: false, title: 'Đối tượng', width : 110 }
            ]],
            onSelect: function (row) {

                $("#pannel_list_lap_thiet_bi").mask("Dữ liệu đang xử lý");

                $('#lap_thiet_bi_TableContainer').jtable('load', { donvi: row.id, toql: row.id, SiteId: row.id, type: row.type, mangvt: maTramVT_DAUNOITB_Currntly });

                maiTyComGirdCurrntly = row.type;

                setTimeout(function () {
                    if (row.type == "dv") {
                        madonviCurrently = row.id ; matoqlCurrently = ""; maiSiteCurrntly = "";
                    }
                    else if (row.type == "toql") {
                        madonviCurrently = row.Idparent; matoqlCurrently = row.id ; maiSiteCurrntly = "";
                    }
                    else {
                        madonviCurrently = row.Idparent_parent; matoqlCurrently = row.Idparent; maiSiteCurrntly = row.id;
                    }



                }, 200);

                setTimeout(function () { $("#pannel_list_lap_thiet_bi").unmask(); }, 2000);

            }
        });


        $('#lap_thiet_bi_TableContainer').jtable({

            title: 'DS LẮP THIẾT BỊ',
            openChildAsAccordion: true,
            actions: {
                listAction: _nameSubDomain + '/TThietbi/ListLapDatTB?donvi=&toql=&SiteId=&type=all&mangvt=PON',
                //deleteAction: function (postData) {
                //    console.log("deleting from custom function...");
                //    return $.Deferred(function ($dfd) {

                //        $("#pannel_list_lap_thiet_bi").mask("Dữ liệu đang được xóa");

                //        $.ajax({
                //            url: _nameSubDomain + '/TThietbi/DeleteLTB',
                //            type: 'POST',
                //            dataType: 'json',
                //            data: postData,
                //            success: function (data) {

                //                $dfd.resolve(data);
                //                setTimeout(function () { $("#pannel_list_lap_thiet_bi").unmask(); }, 600);

                //            },
                //            error: function () {
                //                $dfd.reject();
                //            }
                //        });
                //    });
                //}


            },
            toolbar: {
                hoverAnimation: true, //Enable/disable small animation on mouse hover to a toolbar item.
                hoverAnimationDuration: 60, //Duration of the hover animation.
                hoverAnimationEasing: undefined, //Easing of the hover animation. Uses jQuery's default animation ('swing') if set to undefined.
                items: [
                    {
                        icon: _nameSubDomain + '/Content/images/add.png',
                        text: 'Thêm mới lắp đặt thiết bị',
                        click: function () {
                            _openWinDowLapDatTB();
                        }
                    }
                ] //Array of your custom toolbar items.
            },
            fields: {
                ID: {
                    key: true,
                    edit: false,
                    create: false,
                    //list:false,
                    width: '3%',
                    display: function (tbData) {
                        var $img = $('<img src="' + _nameSubDomain + '/Content/images/1356080636_cog.png" style="cursor:pointer;" title="Xem thông tin cấu hình thiết bị" />');
                        //Open child table when user clicks the image
                        $img.click(function () {
                            $('#lap_thiet_bi_TableContainer').jtable('openChildTable',
                                   $img.closest('tr'), //Parent row
                                   {
                                       title: 'Cấu hình của lắp thiết bị ' + tbData.record.TEN,
                                       toolbar: {
                                           hoverAnimation: true, //Enable/disable small animation on mouse hover to a toolbar item.
                                           hoverAnimationDuration: 60, //Duration of the hover animation.
                                           hoverAnimationEasing: undefined, //Easing of the hover animation. Uses jQuery's default animation ('swing') if set to undefined.
                                           items: [
                                               {
                                                   icon: _nameSubDomain + '/Content/images/add.png',
                                                   text: 'Thêm mới cấu hình thiết bị',
                                                   click: function () {
                                                       _openWinDowAddCauHinhTB(tbData.record.ID, tbData.record.DMTHIETBI, tbData.record.TEN);
                                                   }
                                               }
                                           ] //Array of your custom toolbar items.
                                       },
                                       actions: {
                                           listAction: _nameSubDomain + '/TThietbi/ListCauHinhTB?lapTBId=' + tbData.record.ID,
                                           //deleteAction: _nameSubDomain + '/TThietbi/DeleteCauHinhTB'
                                       },
                                       fields: {
                                           ID: {
                                               key: true,
                                               //create: false,
                                               //edit: false,
                                               list: true,
                                               //title: 'Mã MVT',
                                               width: '3%',
                                               display: function (tbData) {
                                                   var $img_port = $('<img src="' + _nameSubDomain + '/Content/images/ipod_nano_connect.png" style="cursor:pointer;" title="Xem thông tin port cấu hình thiết bị" />');
                                                   //Open child table when user clicks the image
                                                   $img_port.click(function () {
                                                       $('#lap_thiet_bi_TableContainer').jtable('openChildTable',
                                                              $img_port.closest('tr'), //Parent row
                                                              {
                                                                  title: 'DS PORT ' + tbData.record.TEN,
                                                                  //toolbar: {
                                                                  //    hoverAnimation: true, //Enable/disable small animation on mouse hover to a toolbar item.
                                                                  //    hoverAnimationDuration: 60, //Duration of the hover animation.
                                                                  //    hoverAnimationEasing: undefined, //Easing of the hover animation. Uses jQuery's default animation ('swing') if set to undefined.
                                                                  //    items: [
                                                                  //        {
                                                                  //            icon: _nameSubDomain + '/Content/images/add.png',
                                                                  //            text: 'Thêm mới cấu hình thiết bị',
                                                                  //            click: function () {
                                                                  //                //_openWinDowAddCauHinhTB(tbData.record.ID, tbData.record.DMTHIETBI, tbData.record.TEN);
                                                                  //            }
                                                                  //        }
                                                                  //    ] //Array of your custom toolbar items.
                                                                  //},
                                                                  actions: {
                                                                      listAction: _nameSubDomain + '/TThietbi/ListPortByCauHinh?cauhinh=' + tbData.record.ID,
                                                                      updateAction: _nameSubDomain + '/TThietbi/UpdatePortCauHinh',
                                                                      //deleteAction: _nameSubDomain + '/TThietbi/DeleteCauHinhTB'
                                                                  },
                                                                  fields: {
                                                                      ID: {
                                                                          key: true,
                                                                          create: false,
                                                                          edit: false,
                                                                          list: false,
                                                                          //title: 'Mã MVT',
                                                                          //width: '20%'
                                                                      },
                                                                      PORT_SO: {
                                                                          title: 'Port So',
                                                                          width: '30%',
                                                                          //edit: false,
                                                                      },
                                                                      TB_IN: {
                                                                          title: 'TB In',
                                                                          edit: false,
                                                                          width: '30%',
                                                                          display: function (data) {
                                                                              return GetLapDatThietBiById(data.record.TB_IN).TEN;
                                                                          }
                                                                      },
                                                                      TB_OUT: {
                                                                          title: 'Tb Out',
                                                                          edit: false,
                                                                          width: '20%',
                                                                          display: function (data) {
                                                                              return GetLapDatThietBiById(data.record.TB_OUT).TEN;
                                                                          }
                                                                      },
                                                                      TRANGTHAI: {
                                                                          title: 'Trạng thái',
                                                                          width: '20%',
                                                                          display: function (data) {
                                                                              return getTrangthaibyId(data.record.TRANGTHAI).TRANGTHAI1;
                                                                          },
                                                                          options: _nameSubDomain + '/TThietbi/ListTrangThai'
                                                                      },
                                                                      
                                                                  }
                                                              }, function (data) { //opened handler
                                                                  data.childTable.jtable('load');
                                                              });
                                                   });
                                                   //Return image to show on the person row
                                                   return $img_port;
                                               }
                                           },
                                           TEN: {
                                               title: 'Tên Cấu Hình',
                                               width: '30%'
                                           },
                                           SLOT: {
                                               title: 'Slot',
                                               width: '30%'
                                           },
                                           CARD: {
                                               title: 'Card',
                                               width: '20%',
                                               display: function (data) {
                                                   return Get_Card_ById(data.record.CARD).TEN;
                                               }
                                           },
                                           DAULUONG: {
                                               title: 'Đấu Luồng',
                                               width: '20%'
                                           },
                                           UPDATE: {
                                               display: function (data) {
                                                   return '<button title="Edit Record" onclick="return _UpdateCauHinhTB(' + data.record.ID + ', \'' + tbData.record.DMTHIETBI + '\', \'' + tbData.record.TEN + '\')" class="jtable-command-button jtable-edit-command-button"><span></span></button>';
                                               },
                                               create: false,
                                               edit: false,
                                               width: '3%'
                                           },
                                           DETELE: {
                                               display: function (data) {
                                                   return '<button title="delete Record" onclick="return _DeleteCauHinhTB(' + data.record.ID + ',\'' + data.record.TEN + '\',' + tbData.record.ID + ')" class="jtable-command-button jtable-delete-command-button"><span></span></button>';
                                               },
                                               create: false,
                                               edit: false,
                                               width: '3%'
                                           },
                                       }
                                   }, function (data) { //opened handler
                                       data.childTable.jtable('load');
                                   });
                        });
                        //Return image to show on the person row
                        return $img;
                    }
                },
                DMTHIETBI: {
                    title: 'Thiết bị',
                    width: '15%',
                    display: function (data) {
                        return get_dm_thietbi_byId(data.record.DMTHIETBI).TEN;
                    }
                },
                MA: {
                    title: 'Mã',
                    width: '10%'
                },
                TEN: {
                    title: 'Tên',
                    width: '25%'
                },
                //DONVIQL: {
                //    title: 'Đơn vi - tổ QL',
                //    width: '32%',
                //    display: function (data) {
                //        return Get_DonVi_ById(data.record.DONVIQL).TEN + " - " + Get_toql_ById(data.record.TOQL).TEN;
                //    }
                //},
                SITEID: {
                    title: 'Site',
                    width: '15%',
                    display: function (data) {
                        return Get_vnpt_site_ById(data.record.SITEID).TEN;
                    }
                },
                //LOAIPHA: {
                //    title: 'Pha',
                //    width: '15%',
                //    display: function (data) {
                //        if (data.record.LOAIPHA == null) return "";
                //        else return GetLoaiPhaById(data.record.LOAIPHA).TEN;
                //    }
                //},
                NGAYLAP: {
                    title: 'Ngày lắp',
                    width: '15%',
                    display: function (data) {
                        if (data.record.NGAYLAP == null) return "";
                        else return formatDateJson(data.record.NGAYLAP);
                    }
                },
                //GHICHU: {
                //    title: 'Ghi Chú',
                //    width: '25%'
                //},
                DAUNOI: {
                    display: function (data) {

                        var checkTBDauNoi = CheckLapDatTBDaDauNoi(data.record.ID);
                        if (checkTBDauNoi == false) {
                            return '<button type="button" onclick="return dau_noi_thiet_bi(' + data.record.ID + ');"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">Đấu nối</span><span class="l-btn-icon icon-add">&nbsp;</span></span> </button>';
                        }
                        else {
                            return '<button type="button" onclick="return cap_nhat_dau_noi_thiet_bi(' + data.record.ID + ');"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">Đấu nối</span><span class="l-btn-icon icon-edit">&nbsp;</span></span> </button>&nbsp;&nbsp;'
                                  + '<button type="button" onclick="return view_dau_noi_thiet_bi(' + data.record.ID + ');"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">Xem</span><span class="l-btn-icon icon-line">&nbsp;</span></span> </button>';
                        }
                    },
                    width: '5%'

                },
                UPDATE: {
                    display: function (data) {
                        return '<button title="Edit Record" onclick="return _UpdateLapThietBi(' + data.record.ID + ')" class="jtable-command-button jtable-edit-command-button"><span></span></button>';
                    },
                    create: false,
                    edit: false,
                    width: '3%'
                },
                DETELE: {
                    display: function (data) {
                        return '<button title="delete Record" onclick="return _DeleteLapThietBi(' + data.record.ID + ',\'' + data.record.TEN + '\')" class="jtable-command-button jtable-delete-command-button"><span></span></button>';
                    },
                    create: false,
                    edit: false,
                    width: '3%'
                },

            },
            //Initialize validation logic when a form is created
            formCreated: function (event, data) {
                //data.form.find('input[name="TEN"]').addClass('validate[required]');
                data.form.validationEngine();
            },

        });

        $('#lap_thiet_bi_TableContainer').jtable('load');
        
        setTimeout(function () { $("#Layout_dm_lap_dat_cau_hinh_tb").unmask(""); }, 1000);

    });

    function onContextMenu_MVT(e, row) {
        e.preventDefault();
        $(this).treegrid('select', row.id);
        //$('#ContextMenu_mvt_loaisite').menu('show', {
        //    left: e.pageX,
        //    top: e.pageY
        //});
    }

    

    

    function _openWinDowAddCauHinhTB(id, dmTb, name) {
        loadwindowOnContent(_nameSubDomain + "/TThietbi/AddCauHinhTb?lapDatTBid=" + id + "&dmTb=" + dmTb + "&name=" + name, "window_addupdate_cauhinhthietbi", "Cập nhật cấu hình thiết bị của lắp thiết bị " + name, 600, 340);
    }

    function _UpdateCauHinhTB(Id, dmTb, name) {
        loadwindowOnContent(_nameSubDomain + "/TThietbi/UpdateCauHinhTb?chId=" + Id + "&dmTb=" + dmTb + "&name=" + name, "window_addupdate_cauhinhthietbi", "Cập nhật cấu hình thiết bị của lắp thiết bị " + name, 600, 340);
    }

    function _openWinDowLapDatTB()
    {
        loadwindowOnContent(_nameSubDomain + "/TThietbi/_openWinDowLapDatTB?donviqy=" + madonviCurrently + "&_toql=" + matoqlCurrently + "&siteId=" + maiSiteCurrntly + "&mangvt=" + maTramVT_DAUNOITB_Currntly, "window_addupdate_lapthietbiCard_thietbi", "Thêm thông tin lắp thiết bị", 610, 560);
    }

    function _UpdateLapThietBi(id) {

        loadwindowOnContent(_nameSubDomain + "/TThietbi/_openWinDowUpdateLapDatTB?Id=" + id + "&mangvt=" + maTramVT_DAUNOITB_Currntly, "window_addupdate_lapthietbiCard_thietbi", "Cập nhật thông tin lắp thiết bị", 600, 590);

    }

    function dau_noi_thiet_bi(id) {

        loadwindowOnContent(_nameSubDomain + "/TThietbi/DauNoiTB?LapTbId=" + id + "&tramvt=" + maTramVT_DAUNOITB_Currntly, "window_addupdate_dau_noi_thietbi", "Đấu nối thiết bị", 600,350);

    }

    function cap_nhat_dau_noi_thiet_bi(id) {
        loadwindowOnContent(_nameSubDomain + "/TThietbi/CapNhatDauNoiTB?LapTbId=" + id + "&tramvt=" + maTramVT_DAUNOITB_Currntly, "window_addupdate_dau_noi_thietbi", "Cập nhật Đấu nối thiết bị", 600, 350);
    }


    function view_dau_noi_thiet_bi(id) {

        loadwindowOnContent(_nameSubDomain + "/TThietbi/ViewGianDoOnThietBiDauNoi?id=" + id, "window_view_gian_do_dau_noi", "Giản đồ đấu nối", 800, 390);

    }


    function _DeleteLapThietBi(id, name) {

        $("#pannel_list_lap_thiet_bi").mask("Đang kiểm tra dữ liệu");

        $.ajax({ url: _nameSubDomain + "/TThietbi/CheckDeteleLapTb?LaptbId=" + id, async: false }).done(function (data) {

            if (data == true) {

                confirmOptionNormal("Thiết bị đã có port được sử dụng, bạn không thể xóa");
                $("#pannel_list_lap_thiet_bi").unmask();

            }
            else {

                $.confirm({
                    text: "Bạn chắc chắn muốn xóa lắp thiết bị " + name,
                    confirm: function (button) {

                        $("#pannel_list_lap_thiet_bi").mask("Đang xóa dữ liệu");

                        $.ajax({
                            url: _nameSubDomain + '/TThietbi/DeleteLTB?ID=' + id,
                            type: 'GET',
                            dataType: 'json',
                            //data: postData,
                            success: function (data) {

                                //$dfd.resolve(data);
                                $('#lap_thiet_bi_TableContainer').jtable('load', { donvi: madonviCurrently, toql: matoqlCurrently, SiteId: "", type: maiTyComGirdCurrntly, mangvt: maTramVT_DAUNOITB_Currntly });
                                setTimeout(function () { $("#pannel_list_lap_thiet_bi").unmask(); }, 600);

                            },
                            error: function () {
                                //$dfd.reject();
                            }
                        });

                    },
                    cancel: function (button) {

                        $("#pannel_list_lap_thiet_bi").unmask();

                    }
                });

            }

        });

        return false;

    }

    function _DeleteCauHinhTB(idCauHinh, nameCauHinh, idLapTb) {

        $("#pannel_list_lap_thiet_bi").mask("Đang kiểm tra dữ liệu");

        $.ajax({ url: _nameSubDomain + "/TThietbi/checkXoaCauHinhTb?cauhinhId=" + idCauHinh, async: false }).done(function (data) {

            if (data == true) {

                confirmOptionNormal("Thiết bị đã có port được sử dụng, bạn không thể xóa");
                $("#pannel_list_lap_thiet_bi").unmask();

            }
            else {

                $.confirm({
                    text: "Bạn chắc chắn muốn xóa cấu hinh thiết bị " + nameCauHinh,
                    confirm: function (button) {

                        $("#pannel_list_lap_thiet_bi").mask("Đang xóa dữ liệu");

                        $.ajax({
                            url: _nameSubDomain + '/TThietbi/DeleteCauHinhTB?ID=' + idCauHinh,
                            type: 'GET',
                            dataType: 'json',
                            //data: postData,
                            success: function (data) {

                                //$dfd.resolve(data);

                                var $rowReload = $('#lap_thiet_bi_TableContainer').jtable('getRowByKey', idLapTb);
                                $rowReload.next().find('.jtable-child-table-container').jtable('reload')

                                setTimeout(function () { $("#pannel_list_lap_thiet_bi").unmask(); }, 600);

                            },
                            error: function () {
                                //$dfd.reject();
                            }
                        });

                    },
                    cancel: function (button) {

                        $("#pannel_list_lap_thiet_bi").unmask();

                    }
                });

            }

        });

        return false;

    }

</script>